package com.mjw.zstu.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mjw.zstu.ZstuApplication;
import com.mjw.zstu.entity.Company;
import com.mjw.zstu.entity.User;
import com.mjw.zstu.resultDto.JsonResult;
import com.mjw.zstu.service.CompanyService;
import com.mjw.zstu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    CompanyService companyService;

    @RequestMapping("test")
    public String test(){
        return JSON.toJSONString(userService.userMsg());
    }
    @RequestMapping("delete")
    public String delete(){
        return JSON.toJSONString((userService.delete(121)));
    }
    @RequestMapping("json")
    public User get(@RequestBody User user){
        return user;
    }
    @RequestMapping("get")
    public int get(@RequestParam int user_id){
        return user_id;
    }
    @RequestMapping("insert")
    public int insert(@RequestBody User user){
        return userService.insert(user);
    }
    @RequestMapping("file")
    public String getfile(@RequestParam("file") MultipartFile file[]){

        return "success";
    }
    @RequestMapping("compare")
    public JsonResult compare(int user_id,String password){
        return userService.compare(user_id,password);
    }
    @RequestMapping("getAddress")
    public JsonResult<List> getUserAddress(@RequestParam Integer id){
        return new JsonResult<>(200,"获取成功",userService.getUserAddress(id));
    }
    @RequestMapping("login")
    public JsonResult<User> login (@RequestBody  User user){
        User user1=userService.login(user);
        if(user1.getStatus()==1){
            return new JsonResult<>(500,"账号已被拉黑",null);
        }
        if(user1!=null){
            user1.setPassword("");
            return new JsonResult(200,"登录成功",user1);
        }

        return new JsonResult(500,"用户不存在",null);
    }
    @RequestMapping("register")
    public JsonResult<User> register(@RequestBody User user){
        User user1=new User();
        user1.setUsername(user.getUsername());
        User l=userService.find(user1);
        int success=userService.register(user);
        user.setPassword(null);

        if(success==1&&l==null){
            return new JsonResult<User>(200,"注册成功",user);
        }
       return new JsonResult(500,"用户名已存在",null);
    }
    @GetMapping("list")
    public JsonResult getList(){
        return userService.list();
    }
    @RequestMapping("white")
    public JsonResult white(int user_id){
        return userService.white(user_id);
    }
    @RequestMapping("black")
    public JsonResult black(int user_id){
        return userService.black(user_id);
    }
    @RequestMapping("update")
    public JsonResult update(@RequestBody User user){
        return userService.update(user);
    }




}
